﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel.Composition;
using Kooboo.Domain;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(IEntityDeleteCommand<ICustomField>))]
    [Export(typeof(IEntityDeleteCommand<IVariation>))]
    public class ProductAttributeDeleteCommand : EntityDeleteCommand<IProductAttribute>, IEntityDeleteCommand<IVariation>, IEntityDeleteCommand<ICustomField>
    {
        protected override string TableName
        {
            get {
                return "ProductAttribute";
            }
        }

        protected override string BuildCondition()
        {
            var ids = this.EntitySet.Select(i => i.Id).ToArray();
            var where = string.Empty;
            if (ids.Length == 0)
            {
                where = " 1=2 ";
            }
            else
            {
                where = " Id in (" + string.Join(", ", ids) + ") ";
            }

            return where;
        }

        
        IQueryable<IVariation> IEntityDeleteCommand<IVariation>.EntitySet
        {
            get
            {
                return this.EntitySet.OfType<IVariation>();
            }
            set
            {
                this.EntitySet = value.OfType<IProductAttribute>();
            }
        }

     
        IQueryable<ICustomField> IEntityDeleteCommand<ICustomField>.EntitySet
        {
            get
            {
                return this.EntitySet.OfType<ICustomField>();
            }
            set
            {
                this.EntitySet = value.OfType<ICustomField>();
            }
        }
    }
}
